home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12245 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.4 KB

  1. Path: anvil.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Need help with a STRING that won't go away!!!
  5. Date: 29 Mar 1996 15:07:59 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4jhqgfINN678@anvil.ugrad.cs.ubc.ca>
  8. References: <4jh7e2$jjr@abel.cc.sunysb.edu> <Pine.A32.3.91.960329131737.148066B-100000@red.weeg.uiowa.edu>
  9. NNTP-Posting-Host: anvil.ugrad.cs.ubc.ca
  10.  
  11. In article <Pine.A32.3.91.960329131737.148066B-100000@red.weeg.uiowa.edu>,
  12. The Amorphous Mass  <robinson@blue.weeg.uiowa.edu> wrote:
  13.  >On 29 Mar 1996, George Hauser wrote:
  14.  >
  15.  >> In this program I am interested in processing unti the EOF, when
  16.  >> I get to it I don't want to loop again to avoid dumping the
  17.  >> data that still is in the string (line).
  18.  >> 
  19.  >> As it is, it dumps the exact same record that was on the last line twice!
  20.  >> This causes the import to POST an OCEAN FREIGHT amount 2x!!!
  21.  >
  22.  >  You fell in one of the pitfalls awaiting unsuspecting Pascal programmers.
  23.  >  Since feof(stream) only tells you that your last read on stream 
  24.  >returned EOF, the following loop structure doesn't work the way it does 
  25.  >in Pascal:
  26.  >
  27.  >  while (!feof(stream1))
  28.  >  {
  29.  >    fgets(line, stream1);
  30.  >    fputs(line, stream2);
  31.  >  }
  32.  
  33. And with good reason. You see, on many operating systems, it's difficult to
  34. tell whether you are at the end of the file until you actually attempt to
  35. _read_ past the end of the file. C's stdio logic is probably easier to
  36. implement because it reflects that.
  37.  
  38. You try the read, and then if you fail you return EOF.
  39.  
  40.  >  What you want is:
  41.  >
  42.  >  while (fgets(line, stream1))
  43.  >    fputs(line, stream2);
  44.  >
  45.  >  since fgets() returns NULL when it hits EOF.  This reads "while fgets() 
  46.  >reads valid input, fputs() the string read by fgets()."
  47.  
  48. That's probably not the most appropriate way since fgets is used for line-mode
  49. input. For a raw copy, you might want to do an unbuffered fread()/fwrite() or a
  50. getc()/putc() loop. The latter is loop is efficient because it copies data
  51. directly between one stream's buffer and another (getc/putc are frequently
  52. implemented as macros, or are inlined by the compiler so doing it a character
  53. at a time may actually be quite efficient). With fgets() or fread()/fwrite(),
  54. you are putting the data through an intermediate ``bounce buffer'' rather than
  55. pumping it directly between two streams.
  56. -- 
  57.  
  58.